
export default {
  data: {},
  setAll: function (data) {
    this.data = data
  },
  getCache: function (type, id) {
    if (this.data[type]) {
      if (id) {
        let data = null
        this.data[type].some((item) => {
          if (id === item._id) {
            data = item
            return true
          }
        })
        return data
      }
      return this.data[type]
    } else {
      return false
    }
  },
  getAll: function () {
    return this.data
  },
  updateCache: function (type, payload) {
    console.log('更新缓存字段', type, payload)
    if (!this.data[type]) {
      this.data[type] = payload
    } else {
      this.data[type] = payload
    }
    if (type === 'agents') {
      let agentObj = {}
      if (payload === null || payload.length === 0) {
        agentObj = null
      } else if (payload.length > 0) {
        payload.forEach(item => {
          agentObj[item._id] = JSON.parse(JSON.stringify(item))
        })
      }
      this.data['agentObj'] = agentObj
    }
  },
  addOneCache: function (type, payload) {
    // 给某个缓存添加一条数据，使用之前要先确定该缓存没有这条数据
    if (this.data[type]) {
      this.data[type].push(payload)
    }
  },
  deleteCache: function (type) {
    if (this.data[type]) {
      delete this.data[type]
    }
  }
}
